package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObj;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.ITagService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private ITagService tagService;

    public PageInfo<?> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyComment> comments = strategyCommentMapper.selectForList(qo);
        return new PageInfo(comments);
    }

    public void update(StrategyComment strategyComment) {
        if (strategyComment.getState() == 1) {
            strategyComment.setCommendTime(new Date());
        }
        strategyCommentMapper.updateByPrimaryKey(strategyComment);
    }

    public List<StrategyComment> selectByState(Integer state) {
        return strategyCommentMapper.selectByState(state);
    }

    public PageInfo getHeadline(StrategyCommentQueryObj qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyComment> list = strategyCommentMapper.getHeadline(qo);
        return new PageInfo<>(list);
    }

    public StrategyComment getStrategyCommentDetailsById(Long strategyCommentId) {
        return strategyCommentMapper.getStrategyCommentDetailsById(strategyCommentId);
    }

    public PageInfo getStrategyCommentByStrategyId(StrategyCommentQueryObj qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.getStrategyCommentByStrategyId(qo);
        return new PageInfo(list);
    }


    /**
     * 攻略点评添加
     *
     * @return
     */
    @Override
    public void save(Long[] tags, String myTags, StrategyComment strategyComment) {
        strategyComment.setCreateTime(new Date());
         strategyCommentMapper.insert(strategyComment);
        String[] split = myTags.split(" ");
        for (String s : split) {
            Tag tag = new Tag();
            tag.setName(s);
            tagService.save(tag);
            tagService.saveForCommentTag(tag.getId(),strategyComment.getId());
        }
        for (Long tagId : tags) {
            tagService.saveForCommentTag(tagId,strategyComment.getId());
        }

    }

    @Override
    public void updateLikeNum(Long relationId) {
        strategyCommentMapper.updateLikeNum(relationId);
    }

    @Override
    public PageInfo selectByUserId(StrategyCommentQueryObj qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.selectByUserId(qo);
        return new PageInfo<>(list);
    }
}
